//1.导入vue核心包
import Vue from 'vue';
//导入路由包
import VueRouter from 'vue-router';
//导入vue-resourse包
import VueResourse from 'vue-resource';
//导入mint-ui
import mintUI from 'mint-ui';
//导入格式化日期的插件moment
import moment from 'moment';
//导入vue-preview图片预览插件
import vuepreview from 'vue-preview';

//导入cookie.js
import {getCookie} from './kits/cookie.js';


//路由绑定vue
Vue.use(VueRouter);
//绑定vue-resourse
Vue.use(VueResourse);
//mint-ui绑定vue
Vue.use(mintUI);
//vue-preview绑定vue
Vue.use(vuepreview);



//2.导入App.vue组件
import App from './App.vue';
import home from './component/home.vue';
import car from './component/shopcar/car.vue';
import newlist from './component/news/newlist/newlist.vue';
import newinfo from './component/news/newinfo/newinfo.vue';
import photolist from './component/photo/photolist.vue';
import photoinfo from './component/photo/photoinfo.vue';
import goodlist from './component/goods/goodlist.vue';
import goodinfo from './component/goods/goodinfo.vue';
import gooddesc from './component/goods/gooddesc.vue';
import goodcomment from './component/goods/goodcomment.vue';
import userinfo from './component/account/userinfo.vue';
import login from './component/account/login.vue';

//导入url请求的js文件并且作为Vue的方法
import common from '../src/kits/common.js';
Vue.prototype.$common = common;



import getimg from '../src/kits/getphoto.js';
Vue.prototype.$getimg = getimg.getlist;

//导入mint-ui的css组件
import 'mint-ui/lib/style.min.css';
//导入mui的css样式
import '../statics/mui/css/mui.css';
//导入公共样式
import '../statics/css/base.css';
//定义路由,定义路由规则
var router = new VueRouter({
    routes: [{
            path: '/',
            redirect: '/home'
        },
        {
            path: '/home',
            component: home
        },
        {
            path: '/car',
            component: car
        },
        {
            path: '/news/newlist',
            component: newlist
        },
        {
            path: '/news/newinfo/:getid',
            component: newinfo
        },
        {
            path: '/photo/photolist',
            component: photolist
        },
        {
            path: '/photo/photoinfo/:getid',
            component: photoinfo
        },
        {
            path: '/goods/goodlist',
            component: goodlist
        },
        {
            path: '/goods/goodinfo/:getid',
            component: goodinfo
        },
        {
            path: '/goods/gooddesc/:getid',
            component: gooddesc
        },
        {
            path: '/goods/goodcomment/:getid',
            component: goodcomment
        },
        {
            path: '/goods/goodcomment/:getid',
            component: goodcomment
        },
        { 
            path: '/login', 
            component: login 
        },
        {
            path: '/userinfo',
            component: userinfo,
            meta: {
                requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
            },
            // 进入该路由前会执行
            beforeEnter: (to, from, next) => {
                if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
                    if (getCookie('session')) {
                        // 通过cookie获取当前的token是否存在
                        next();
                    } else {
                        next({
                            path: '/login',
                            query: {
                                redirect: to.fullPath
                            } // 将跳转的路由path作为参数，登录成功后跳转到该路由
                        })
                    }
                } else {
                    next();
                }
            }
        }
    ]
})
//3.利用vue对象进行渲染和解析
new Vue({
    el: '#app',
    // render:function(create){create(App)}  //es5语法
    render: c => c(App), //es6语法
    //实例化路由
    router
})

//自定义过滤器
Vue.filter('timedmf', function (input, formatString) {
    formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
    return moment(input).format(formatString);
})